package org.zjvis.graph.analysis.service.http;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.zjvis.datascience.common.vo.PluginVO;
import org.zjvis.graph.analysis.service.exception.GraphAnalysisException;
import org.zjvis.graph.analysis.service.util.FileUtil;

import javax.annotation.PostConstruct;

@Service
public class PluginRegister {
    private static final Logger logger = LoggerFactory.getLogger("PluginRegister");

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Value("${plugin.route}")
    private String route;


    @PostConstruct
    public void enable() {
        String jsonStr = FileUtil.readJsonFile("meta.plugin");
        PluginVO plugin = JSONObject.parseObject(jsonStr).toJavaObject(PluginVO.class);
        plugin.setUrl(route);
        try {
            logger.info("PluginRegister request");
            JSONObject resp = restTemplateUtil.postWithNoCookie("/plugin/enable", plugin);
            logger.info("PluginRegister success");
        } catch (Exception e) {
            logger.error("PluginRegister error", e);
            throw new GraphAnalysisException(e);
        }
    }
}
